const App = getApp();
const api = require("../../../utils/api.js");
Page({
    /**
   * json:
   * 0：题目
   * 1-4选项
   * 5：正确答案
   * 6：答案文字解析
   * 7：难度指数
   * 8：考点
   * 9：题型
   * 10：视频解析地址
   */
  data: {
    json:[],
    err:[],
    sum:'',
    swiperIndex:0,//页面索引
    isok:'',//答题结果
    isSuccess:'',//正确答案
    chooseValue:[],
    choose:1,
    colllist:[],
    title:'',
    list:[],
    answerMask: false,
    sheet: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
   this.getParams();

  },
  onReady:function(){
    this.collection = this.selectComponent("#collection")
  },

  getParams:function(){
    const that = this;
    let eventChannel = this.getOpenerEventChannel();
    // 监听aishang事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('json', function (json) {
      that.setData({
        json:json,
      })
      console.log("json加载完成")
    })
    eventChannel.on('err', function (err) {
      that.setData({
        err:err,
      })
      console.log("err加载完成")
    })
    eventChannel.on('chooseValue', function (chooseValue) {
      that.setData({
        chooseValue:chooseValue,
      })
      console.log("chooseValue加载完成")
    })

    eventChannel.on('colllist', function (colllist) {
      that.setData({
        colllist:colllist,
      })
      console.log("colllist加载完成")
    })
    eventChannel.on('index', function (index) {
      if(index!=0){
        that.setData({
          swiperIndex:index-1,
        })
      }
    })
    eventChannel.on('title', function (title) {
      that.setData({
        title:title,
      })
      console.log("title加载完成")
      that.initData()
      that.isokQuestion();
    })
  },
  moveServerProSwiper:function(e){
   const that = this;
   that.setData({
    swiperIndex: e.detail.current,
   })
   this.isokQuestion();
  },
  isokQuestion:function(){
    console.log("isokQuestion开始执行")
    const that = this;
    var err = that.data.err;
    var index = that.data.swiperIndex;
    if(err[index]!=0){
      that.setData({
        isok:'回答错误',
      })
    }else{
      that.setData({
        isok:'回答正确',
      })
    }
    var json = that.data.json;
    var chooseValue = that.data.chooseValue;
    var list = json[index][5];
    var myquestion;
    switch(list){
      case '1': list = 'A';break;
      case '2': list = 'B';break;
      case '3': list = 'C';break;
      case '4': list = 'D';break;
    }
    switch(chooseValue[index]){
      case 1: myquestion = 'A';break;
      case 2: myquestion = 'B';break;
      case 3: myquestion = 'C';break;
      case 4: myquestion = 'D';break;
      default: myquestion=''
    }
    that.setData({
      isSuccess:list,
      myquestion:myquestion,
    })
  },
  chooseFontChange:function(){
    const that = this;
    that.setData({
      choose:1,
    })
  },
  chooseVideoChange:function(){
    const that = this;
    that.setData({
      choose:2,
    })
  },
  showCollection: function() {
    const that = this;
    var parmas = that.data.colllist;
   
     if (parmas[that.data.swiperIndex].state == 0) {
        parmas[that.data.swiperIndex].state = 1;
       
    } else {
        
        parmas[that.data.swiperIndex].state = 0;
    }
    that.setData({
        colllist: parmas,
    }) 
    this.collection.showCollection(parmas[that.data.swiperIndex].state);
    var url = "/study/collectQuestion"

    api.sendPosts(url, parmas[that.data.swiperIndex],
    function(res) {
        console.log(res)
    })

},
initData:function(){
  console.log("init开始执行")
   const that =this;
  if(that.data.title=="错题解析"){
    var json = that.data.json;
    var err = that.data.err;
    var chooseValue = that.data.chooseValue;
    var errs =[];
    var list = [];
    var choose = [];
    var sum = 0;
    for(var i=0;i<json.length;i++){
      if(err[i]!=0){
        //说明是错题
        sum++;
        list.push(json[i])
        that.setData({
          sum:sum,
        })
      }
    }
    for(var i=0,t=0;i<err.length;i++){
      if(err[i]!=0){
        //说明是错题
        errs[t]=err[i]
        t++;
      }
    }
    for(var i=0,t=0;i<chooseValue.length;i++){
      if(chooseValue[i] != json[i][5]){
        choose[t]=chooseValue[i];
        t++;
      }
    }
    that.setData({
      json:list,
      err:errs,
      chooseValue:choose,
    })
  }else{
    that.setData({
      sum:that.data.json.length,
    })
  }
 
},
//答题卡
submitBtnHandle: function(e) {
  const that = this;
  var sheets = [];
  var chooseValue = that.data.chooseValue;

  for (var i = 0; i < this.data.sum; i++) {
      sheets.push(i + 1)
  }
  console.log("点击答题卡")
  this.setData({
  answerMask: !this.data.answerMask,
  sheet: sheets,
  })
},

  //答题卡跳转
  goIndexQuestion: function(e) {
    const that = this;
    var index = e.currentTarget.dataset.indexs - 1;
    that.setData({
        swiperIndex: index,
        answerMask: !that.data.answerMask,
    })

},
})